package com.kuang.controller;


import com.alibaba.fastjson.JSONObject;
import com.kuang.pojo.User;
import com.kuang.pojo.loginLog;
import com.kuang.service.LogService;
import com.kuang.service.UserService;
import com.kuang.utils.voCommon;
import javafx.scene.chart.PieChart;
import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @Qualifier("LogServiceImpl")
    private LogService logService;

    //查询全部用户
    @ResponseBody
    @RequestMapping(value = "/allUser",produces = "text/html;charset=utf-8")
    public String User(HttpSession session){
        List<User> list = userService.queryAllUser();
            int count=list.size();
            System.out.print("数据"+count);
            return "{ \"code\":200,\"message\":\"成功\",\"data\":" + JSONObject.toJSONString(list) + ",\"count\" :"+count+"}";
        }

    //登录的实现
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username,String password, HttpSession session) {
        User user = userService.queryUserByName(username, password);
        if (user==null) {
            String msg="0";
             return msg;
        } else {
            String msg="1";
            session.setAttribute("userInfo",user);
            return msg;
        }
    }
    //退出登录
    @RequestMapping("/goOut")
    public String goOut(HttpSession session){
        session.removeAttribute("username");
        return "/login";
    }

    //修改密码
    @RequestMapping("/pwd")
    public String pwd(String password,Model model,HttpSession session){
        //获取session中的username
        User user=(User)session.getAttribute("username");
        String username=user.getUsername();

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("username",username);
        map.put("pwd",password);
        userService.updateUserPassword(map);
        return "";
    }

    //注册
    @RequestMapping("/register")
    @ResponseBody
    public String register(User user){
        userService.addUser(user);
        String msg="1";
        return msg;
    }
    //删除用户
    @ResponseBody
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
       userService.deleteUser(id);
       String msg="1";
       return msg;
    }
}

